﻿
function isEmpty(obj) {
    if (typeof obj == 'undefined' || obj === null || obj === '' || obj.value == "") return true;
    if (typeof obj == 'number' && isNaN(obj)) return true;
    if (obj instanceof Date && isNaN(Number(obj))) return true;
    return false;
}



function PreSaveAction(travelDt, returnDt) {

    var date1 = travelDt;
    var date2 = returnDt;
    var arrDate1 = date1.value.split("/");
    var useDate1 = new Date(arrDate1[2], arrDate1[1] - 1, arrDate1[0]);

    var arrDate2 = date2.value.split("/");
    var useDate2 = new Date(arrDate2[2], arrDate2[1] - 1, arrDate2[0]);
    if (useDate1 > useDate2) {
       
        return false; //
    }
    return true;
}

function IsValidDateFormat(sText) {
    var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
    return reDate.test(sText);
}

function DaysCalculation(ReturnDt, travalDt, leaveDays) {

    var date1 = ReturnDt;
    var date2 = travalDt;
    var arrDate1 = date1.value.split("/");
    var useDate1 = new Date(arrDate1[2], arrDate1[1] - 1, arrDate1[0]);
    var arrDate2 = date2.value.split("/");
    var useDate2 = new Date(arrDate2[2], arrDate2[1] - 1, arrDate2[0]);

    if (useDate1 >= useDate2) {
        var ONE_DAY = 1000 * 60 * 60 * 24
        var d1 = useDate1.getTime()
        var d2 = useDate2.getTime()
        var diff = Math.abs(d1 - d2)
        var days = Math.round(diff / ONE_DAY);
        leaveDays.value = days + 1;
        leaveDays.value = leaveDays.value;
    }
    else {
        leaveDays.value = "";
    }
}


function DaysCalculations(ReturnDt, travalDt) {
    var date1 = new Date(ReturnDt);
    var date2 = new Date(travalDt);

    var ONE_DAY = 1000 * 60 * 60 * 24
    var d1 = date1.getTime()
    var d2 = date2.getTime()
    var diff = Math.abs(d1 - d2)
    var days = Math.round(diff / ONE_DAY);
    alert(days);

}

function CompareDate(fromDate, toDate) {

    var endDate = new Date(toDate);
    var startDate = new Date(fromDate);
    if (fromDate != '' && toDate != '' && startDate > endDate) {
        alert('end date should be greater than or equal to start date');
        documet.getElementById('txtToDate').value = "";
        return false;
    }
    else if (fromDate == '') {
        alert('Please enter start date!');
        return false;
    }
    else if (toDate == '') {
        alert('Please enter end date!');
        return false;
    }
}

function fn_DateCompare(DateA, DateB) {     // this function is good for dates > 01/01/1970

    var a = DateA;
    var b = DateB;

    var fDate, lDate, cDate;
    fDate = Date.parse(DateA.value);
    lDate = Date.parse(DateB.value);

    if (fDate > lDate)
        alert("Travel date should be less than return date");
    else
        alert("ok");

    var msDateA = Date.UTC(a.getFullYear(), a.getMonth() + 1, a.getDate());
    var msDateB = Date.UTC(b.getFullYear(), b.getMonth() + 1, b.getDate());

    if (parseFloat(msDateA) < parseFloat(msDateB))
        return -1;  // lt
    else if (parseFloat(msDateA) == parseFloat(msDateB))
        return 0;  // eq
    else if (parseFloat(msDateA) > parseFloat(msDateB))
        return 1;  // gt
    else
        return null;  // error
}


function ValidateDate(id, lblBuildDateid) {
    var rval = true;
    var dtControl = document.getElementById(id);
    if (dtControl.value != '') {
        isDateEmpty = false;
        document.getElementById(lblBuildDateid).innerText = "";
        document.getElementById(lblBuildDateid).style.display = 'none';
    }
    else {
        rval = false;
        isDateEmpty = true;
        document.getElementById(lblBuildDateid).innerText = "Required!";
    }
    if (!isDateEmpty) {
        if (!IsValidDateFormat(dtControl.value)) {
            isValidDateFormat = false;
            rval = false;
            document.getElementById(lblBuildDateid).innerText = "Date format should be dd/mm/yyyy.";
            document.getElementById(lblBuildDateid).style.display = 'block';
        }
        else {
            isValidDateFormat = true;
            document.getElementById(lblBuildDateid).innerText = "";
            document.getElementById(lblBuildDateid).style.display = 'none';
        }
    }

    rval = datecompare('<%= dtPickupDate.Controls[0].ClientID  %>', '<%= dtBookingDate.Controls[0].ClientID  %>');
    return rval;
}





function IsDateValid(obj) {
    var str = obj.value.split('/');
    var dtDeliveryDate = new Date(parseInt(str[2], 10), parseInt(str[1], 10) - 1, parseInt(str[0], 10));
    var fullDate = new Date();
    //Thu May 19 2011 17:25:38 GMT+1000 {} 
    //convert month to 2 digits 
    var twoDigitMonth = (fullDate.getMonth() > 9) ? (fullDate.getMonth() + 1) : '0' + (fullDate.getMonth() + 1);
    var twoDigitMonthDilDt = (dtDeliveryDate.getMonth() > 9) ? (dtDeliveryDate.getMonth() + 1) : '0' + (dtDeliveryDate.getMonth() + 1);
    var currentDate = new Date(fullDate.getFullYear(), parseInt(twoDigitMonth, 10) - 1, fullDate.getDate() - 1);
    var strDate = '01/01/1990';
    var strDD = strDate.split('/');
    var startDate = new Date(parseInt(strDD[2], 10), parseInt(strDD[1], 10) - 1, parseInt(strDD[0], 10));
    //        if (dtDeliveryDate < currentDate) {            
    //            return false;
    //        }

    if (dtDeliveryDate < startDate)
        return false;
    return true;
}

function dateComparision() {
    if (strDate && strDelDate) {
        //alert(1);
        var regExp = /(\d{1,2})\/(\d{1,2})\/(\d{2,4})/;
        if (parseInt(strDelDate.replace(regExp, "$3$2$1")) < parseInt(strDate.replace(regExp, "$3$2$1"))) {
            ErrorMsg = "Expected date of delivery should not be less than current date"
            //alert("Expected date of delivery should not be less than current date");
            objFocus = document.getElementById('<%=dtpDeliveryDate.Controls[0].ClientID %>');
            rVal = false;
        }
    }
}

function datecompare(datePickup, dateBooking) {
    var rval = true;
    var str = document.getElementById(datePickup).value.split('/');
    var dtDeliveryDate = new Date(parseInt(str[2], 10), parseInt(str[1], 10) - 1, parseInt(str[0], 10));
    var fullDate = new Date();
    //Thu May 19 2011 17:25:38 GMT+1000 {} 
    //convert month to 2 digits 
    var twoDigitMonth = (fullDate.getMonth() > 9) ? (fullDate.getMonth() + 1) : '0' + (fullDate.getMonth() + 1);
    var twoDigitMonthDilDt = (dtDeliveryDate.getMonth() > 9) ? (dtDeliveryDate.getMonth() + 1) : '0' + (dtDeliveryDate.getMonth() + 1);
    var currentDate = new Date(fullDate.getFullYear(), parseInt(twoDigitMonth, 10) - 1, fullDate.getDate());
    var strDate = document.getElementById(dateBooking).value;
    var strDD = strDate.split('/');
    var startDate = new Date(parseInt(strDD[2], 10), parseInt(strDD[1], 10) - 1, parseInt(strDD[0], 10));
    if (startDate < currentDate) {
        document.getElementById('<%= lblDateBooking.ClientID  %>').innerText = "Booking Date should not be less than Today's date.";
        document.getElementById('<%= lblDateBooking.ClientID  %>').style.display = 'block';
        rval = false;
    }
    else {
        document.getElementById('<%= lblDateBooking.ClientID  %>').innerText = "";
        document.getElementById('<%= lblDateBooking.ClientID  %>').style.display = 'none';
    }
    if (dtDeliveryDate < startDate) {
        document.getElementById('<%= lblDatePickupError.ClientID  %>').innerText = "Pick Up Date should not be less than Booking date.";
        document.getElementById('<%= lblDatePickupError.ClientID  %>').style.display = 'block';
        rval = false;
    }
    else {
        document.getElementById('<%= lblDatePickupError.ClientID  %>').innerText = "";
        document.getElementById('<%= lblDatePickupError.ClientID  %>').style.display = 'none';
    }
    return rval;
}


